﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace PersonaLibrary.Converters
{
	internal sealed class FromBytesToImage : IValueConverter
	{
		public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			Binary binary = value as Binary; 
			if (binary == null)
				return null;
			byte[] bytes = binary.ToArray();

			BitmapImage result = new BitmapImage();
			result.BeginInit();
			result.CreateOptions = BitmapCreateOptions.None;
			result.CacheOption = BitmapCacheOption.Default;
			result.StreamSource = new MemoryStream(bytes);
			result.EndInit();
			return result;
		}

		public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}
}
